﻿using Coolvis.UI.Theme.Console;
using CPPEI.Coolzon.Event.NCL;
using CPPEI.Coolzon.Event.UI;
using CPPEI.Coolzon.ServiceProxy;
using CPPEI.Coolzon.UI;
using CPPEI.Coolzon.UI.Views;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Sunboxsoft.ChanceData.Theme.Green.Console
{
    /// <summary>
    /// Interaction logic for GreenLoginView.xaml
    /// 用于做登录验证
    /// </summary>
    public partial class LoginView : BaseLoginView
    {
        /// <summary>
        /// 登陆类型
        /// </summary>
        public string SolutionSelectionMode { get; set; }

        private bool _isAutoLogin = false;

        public LoginView()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 创建登录界面，并且直接进行登录认证
        /// </summary>
        /// <param name="userAccount">账号</param>
        /// <param name="userPassword">密码</param>
        /// <param name="solutionSelectionMode">方案选择模式</param>
        public LoginView(string userAccount, string userPassword, string solutionSelectionMode)
        {
            InitializeComponent();

            _ctlEditAccount.Text = userAccount;
            _ctlEditPassword.Password = userPassword;
            this.SolutionSelectionMode = solutionSelectionMode;
            _isAutoLogin = true;
        }

        public LoginView(string userAccount, string userPassword)
        {
            InitializeComponent();

            _ctlEditAccount.Text = userAccount;
            _ctlEditPassword.Password = userPassword;
            _isAutoLogin = true;
        }

        protected override void OnActive()
        {
            base.OnActive();
            _ctlEditAccount.Focus();
            if(_isAutoLogin)
            {
                DoActionLogin(_ctlEditAccount.Text, _ctlEditPassword.Password);
            }
        }

        protected override void OnDeactive()
        {
            base.OnDeactive();
        }

        protected override void OnClose()
        {
            base.OnClose();
        }

        /// <summary>
        /// 点击登录按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ctlBtnOK_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(_ctlEditAccount.Text))
            {
                return;
            }
            DoActionLogin(_ctlEditAccount.Text, _ctlEditPassword.Password);
        }

        private void ctlBtnCancel_Click(object sender, RoutedEventArgs e)
        {
            DoActionCancel();
        }

        protected override void OnPreviewKeyDown(KeyEventArgs e)
        {
            if(e.Key == Key.Enter)
            {
                ctlBtnOK_Click(null, null);
            }

            base.OnPreviewKeyDown(e);
        }
    }
}
